﻿using System;
using System.Collections;
using System.Collections.Generic;
using AiLuWeb.Model;
using Ajeelee.Core.Database;
using Ajeelee.Core.Utils;

namespace AiLuWeb.Business
{
    public interface IManagementTeamBusinessFactory
    {
        IManagementTeamBusiness CreateManagementTeamBusiness();
    }
    public class ManagementTeamBusinessFactory: BusinessFactory, IManagementTeamBusinessFactory
    {
        public IManagementTeamBusiness CreateManagementTeamBusiness()
        {
            return new Default.ManagementTeamBusiness();
        }    
    }
    ///<summary>
    /// interface
    ///</summary>
    public interface IManagementTeamBusiness : IBusinessBase<ManagementTeam, int>
    {
        ///<summary>
        ///根据ID值读取ManagementTeam实体
        ///</summary>
        ManagementTeam GetById(int id);
        ///<summary>
        ///根据ManagementTeam实体插入数据库
        ///</summary>
        object Insert(ManagementTeam entity);
        ///<summary>
        ///根据ManagementTeam实体更新数据库
        ///</summary>
        int Update(ManagementTeam entity);
        ///<summary>
		///<summary>
        ///根据HashTable更新数据库(只更新指定字段)
        ///</summary>
        int UpdateSomeVal(Hashtable table);        
        ///<summary>
        ///根据ManagementTeam实体删除数据库
        ///</summary>
        int Delete(ManagementTeam entity);
        ///<summary>
        ///根据ManagementTeam实体ID删除数据库
        ///</summary>
        int Delete(int id);
        /// <summary>
        /// 读取所有记录
        /// </summary>
        IList<ManagementTeam> GetAllManagementTeam();
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        IList<ManagementTeam> GetAllManagementTeam(object objQuery);
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录(先查所有再用List手动处理分页)
        /// </summary>
        IList<ManagementTeam> GetAllManagementTeam(object objQuery, PageStructure page);
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		IList<ManagementTeam> GetAllManagementTeamByPage(Hashtable table, PageStructure page);
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        int GetAllCount(Hashtable table);
		
    }
}
namespace AiLuWeb.Business.Default
{
    ///<summary>
    ///
    ///</summary>
    public class ManagementTeamBusiness : BusinessBase<ManagementTeam, int>, IManagementTeamBusiness
    {
	
        ///<summary>
        ///根据ID值读取ManagementTeam实体
        ///</summary>
        public ManagementTeam GetById(int id)
        {
            if(id == null) 
                throw new BusinessException("Invalid_Id_Value");
            ManagementTeam entity = GetById("ManagementTeam.ManagementTeam_GetById", id);
            return entity;
        }
        ///<summary>
        ///根据ManagementTeam实体插入数据库
        ///</summary>
        public object Insert(ManagementTeam entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Insert("ManagementTeam.ManagementTeam_Insert", entity);
        }
        ///<summary>
        ///根据ManagementTeam实体更新数据库
        ///</summary>
        public int Update(ManagementTeam entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Update("ManagementTeam.ManagementTeam_Update", entity);
        }
        ///<summary>
        ///根据Hashtable实体更新数据库(只更新指定字段)
        ///</summary>
        public int UpdateSomeVal(Hashtable entity)
        {
            return Update("ManagementTeam.ManagementTeam_UpdateSomeVal", entity);
        }
        ///<summary>
        ///根据ManagementTeam实体删除数据库
        ///</summary>
        public int Delete(ManagementTeam entity)
        {
            if (entity.Id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            return Delete("ManagementTeam.ManagementTeam_Delete", entity);
        }
        ///<summary>
        ///根据ManagementTeam实体ID删除数据库
        ///</summary>
        public int Delete(int id)
        {
            if (id == null)
            {
                throw new BusinessException("Invalid_Id_Value");
            }
            ManagementTeam entity = new ManagementTeam { Id = id };
            entity.Id = id;
            return Delete("ManagementTeam.ManagementTeam_Delete", entity);
        }
        /// <summary>
        /// 读取所有记录
        /// </summary>
        public IList<ManagementTeam> GetAllManagementTeam()
        {
            return GetAllManagementTeam(null);
        }
        /// <summary>
        /// 根据objQuery条件读取所有记录，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public IList<ManagementTeam> GetAllManagementTeam(object objQuery)
        {
            return GetAllManagementTeam(objQuery, null);
        }
        /// <summary>
        /// 根据objQuery查询条件和page分页条件读取相应分页记录
        /// </summary>
        public IList<ManagementTeam> GetAllManagementTeam(object objQuery, PageStructure page)
        {
            return QueryForList("ManagementTeam.ManagementTeam_GetAll", objQuery, page);
        }
		/// <summary>
        /// 根据table查询条件和page分页条件读取相应分页记录(每次只查需要数据)
        /// </summary>
		public IList<ManagementTeam> GetAllManagementTeamByPage(Hashtable table, PageStructure page)
        {
			if (page.CurrPage == 0 || page.PageSize == 0)
			{
				return new List<ManagementTeam>();
			}
			
			page.Counts = GetAllCount(table);
            page.PageCount = (page.Counts % page.PageSize == 0) ? page.Counts / page.PageSize : page.Counts / page.PageSize + 1;
			
			if (page.CurrPage == 1)
			{
            	table.Add("TopNum", page.PageSize);
				return GetAllManagementTeam(table);
			}
			
			int max = (page.CurrPage - 1) * page.PageSize;
			if (max < 500)
            {
            	table.Add("maxsize", max);
				table.Add("pagesize", page.PageSize);
				return QueryForList("ManagementTeam.ManagementTeam_GetByPageNotIn", table, null);
            }
			else
			{
				table.Add("pagesize", page.Counts - max > page.PageSize ? page.PageSize : page.Counts - max);
				table.Add("currcount", page.CurrPage * page.PageSize);
				if (table.ContainsKey("OrderDirection"))
                {
                    table["OrderDirection"] = table["OrderDirection"].ToString().ToUpper();
                }
				return QueryForList("ManagementTeam.ManagementTeam_GetByPage3Top", table, null);
			}
        }
        /// <summary>
        /// 根据objQuery条件读取条数，objQuery可用值类型、hashtable、类实例
        /// </summary>
        public int GetAllCount(Hashtable table)
        {
            return Convert.ToInt32(QueryForScalar("ManagementTeam.ManagementTeam_GetAllCount", table));
        }
    }
}
